package com.xian.hbase;



import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;

import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

public class MyTest {
    public static void main(String[] args) throws Exception {





        System.out.println("hello hbase !");

        BufferedReader reader = new BufferedReader(new FileReader("E:/temp/a.txt"));
        String line = null;
        while ((line = reader.readLine()) !=null) {
            System.out.println(line);
            //byte[] latin1s = line.getBytes("latin1");
            byte[] latin1s = line.getBytes("ISO-8859-1");
            String s = new String(latin1s, "utf-8");
            //byte[] latin1s = line.getBytes();
            //String s = new String(latin1s, "gbk");
            System.out.println("s========="+new String(latin1s));
        }



        System.out.println(new String(" `datanodeid` int COMMENT '?????', ".getBytes("ISO-8859-1"), "UTF-8"));
        System.out.println(new String(" `datanodeid` int COMMENT '?????', ".getBytes("cp1252"), "UTF-8"));


        String s = convertCharset("`datanodeid` int COMMENT '?????',");
        System.out.println(s);


        Charset charsetIso88591 = CharsetUtil.CHARSET_ISO_8859_1;
        String str = StrUtil.str(" `datanodeid` int COMMENT '?????', ", charsetIso88591);
        str = StrUtil.str(str, CharsetUtil.CHARSET_GBK);
        System.out.println("str=="+str);

        String s1 = convertEncodingFormat("  `datanodeid` int COMMENT '?????', ", "cp1252", "ISO-8859-1");
        System.out.println(s1);
        String s2 = convertEncodingFormat(s1, "ISO-8859-1", "utf-8");
        System.out.println(s2);


    }

    /**
     * 将一段错误解码的字符串重新解码
     */
    public static String convertEncodingFormat(String str, String formatFrom, String FormatTo) {
        String result = null;
        if (!(str == null || str.length() == 0)) {
            try {
                result = new String(str.getBytes(formatFrom), FormatTo);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String convertCharset(String s) {
        if (s != null) {
            try {
                int length = s.length();
                byte[] buffer = new byte[length];
                // 0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90
                // to 0x0090, and 0x9d to 0x009d.
                for (int i = 0; i < length; ++i) {
                    char c = s.charAt(i);
                    if (c == 0x0081) {
                        buffer[i] = (byte) 0x81;
                    } else if (c == 0x008d) {
                        buffer[i] = (byte) 0x8d;
                    } else if (c == 0x008f) {
                        buffer[i] = (byte) 0x8f;
                    } else if (c == 0x0090) {
                        buffer[i] = (byte) 0x90;
                    } else if (c == 0x009d) {
                        buffer[i] = (byte) 0x9d;
                    } else {
                        buffer[i] = Character.toString(c).getBytes("CP1252")[0];
                    }
                }
                String result = new String(buffer, "UTF-8");
                return result;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }




}
